/*
 * Copyright (c) 2017-present Ilhasoft.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package br.com.ilhasoft.support.validation.validator;

import br.com.ilhasoft.support.validation.rule.EmptyRule;
import br.com.ilhasoft.support.validation.rule.MaxLengthRule;
import br.com.ilhasoft.support.validation.rule.MinLengthRule;
import br.com.ilhasoft.support.validation.rule.Rule;
import br.com.ilhasoft.support.validation.util.ErrorMessageHelper;
import ohos.agp.components.Text;

/**
 * Created by john-mac on 5/14/16.
 */
public class LengthValidator {
    /**
     * 最小长度验证
     * @param view view
     * @param content content
     * @param minLength minLength
     * @param errorMessage errorMessage
     * @param autoDismiss autoDismiss
     * @return boolean true&false
     */
    public static boolean bindingMinLength(Text view, String content,int minLength,
        String errorMessage, boolean autoDismiss) {
        String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
            errorMessage,"Invalid field. The minimum number of characters %1$d", minLength);
        Rule rule = new MinLengthRule(view, content , minLength, handledErrorMessage);
        return rule.validate();
    }

    /**
     * 最大长度
     * @param view view
     * @param content content
     * @param maxLength minLength
     * @param errorMessage errorMessage
     * @param autoDismiss autoDismiss
     * @return boolean true&false
     */
    public static boolean bindingMaxLength(Text view, String content,int maxLength,
        String errorMessage, boolean autoDismiss) {
        String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
            errorMessage,"Invalid field. The maximum number of characters %1$d", maxLength);
        Rule rule = new MaxLengthRule(view, content , maxLength, handledErrorMessage);
        return rule.validate();
    }

    /**
     * 空字符验证
     * @param view view
     * @param content content
     * @param empty empty
     * @param errorMessage errorMessage
     * @param autoDismiss autoDismiss
     * @return boolean true&false
     */
    public static void bindingEmpty(Text view, String content,boolean empty,
        String errorMessage, boolean autoDismiss) {
        String handledErrorMessage = ErrorMessageHelper.getStringOrDefault(view,
            errorMessage,"This field can't be empty");
        Rule rule = new EmptyRule(view, content , empty, handledErrorMessage);
        rule.validate();
    }


}
